package com.catm.haitao.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;


@SuppressLint("SetJavaScriptEnabled")
public class CustomWebView extends WebView {

	public final static String TAG = "CustomWebView";
	private Context context;

	public CustomWebView(Context context) {
		//注意，这里的super()传入的context应为activity的context，不然无法展示H5的弹出框
		super(context);
		this.context = context.getApplicationContext();
		this.setup();
	}

	public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context = context.getApplicationContext();
		this.setup();
	}

	public CustomWebView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context.getApplicationContext();
		this.setup();
	}

	public CustomWebView(Context context, AttributeSet attrs, int defStyle, boolean var4){
		super(context, attrs, defStyle, var4);
		this.context = context.getApplicationContext();
		this.setup();
	}

	public void setup() {

		this.setInitialScale(0);
		this.setVerticalScrollBarEnabled(false);
        this.setHorizontalScrollBarEnabled(false);
		this.requestFocusFromTouch();

		// Enable JavaScript
		WebSettings settings = this.getSettings();
		settings.setJavaScriptEnabled(true);
		settings.setJavaScriptCanOpenWindowsAutomatically(true);
		settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);

		// Enable database
		settings.setDatabaseEnabled(true);
		String databasePath = context.getDir("database", Context.MODE_PRIVATE).getPath();
		settings.setDatabasePath(databasePath);
		// Enable DOM storage
		settings.setDomStorageEnabled(true);
		// Enable built-in geolocation
		settings.setGeolocationDatabasePath(databasePath);
		settings.setGeolocationEnabled(true);

		settings.setAppCacheMaxSize(1024 * 1024 * 8);// 设置缓冲大小，我设的是8M
		String appCacheDir = context.getDir("cache", Context.MODE_PRIVATE).getPath();
		settings.setAppCachePath(appCacheDir);
		settings.setAllowFileAccess(true);
		settings.setAppCacheEnabled(true);
		settings.setCacheMode(WebSettings.LOAD_DEFAULT);

        this.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
	}
}